package interpreter.builtin.functions;

import interpreter.Callable;

import java.util.List;

public class NumberEqualPredicate implements Callable {

	@Override
	public Object call(List<Object> arguments) {
		checkThatAllArgumentsAreExactComplexNumbers(arguments);
		Object first = arguments.get(0);
		for (int i = 1; i < arguments.size(); i++)
			if (!(first.equals(arguments.get(i))))
				return false;
		return true;
	}

	private void checkThatAllArgumentsAreExactComplexNumbers(
			List<Object> arguments) {
		for (Object arg : arguments)
			if (!(arg instanceof math.exact.Complex))
				throw new OnlyExactNumbersCanBeComparedForEquality(arg);
	}

	@SuppressWarnings("serial")
	public class OnlyExactNumbersCanBeComparedForEquality extends
			RuntimeException {
		private Object illegal;

		public OnlyExactNumbersCanBeComparedForEquality(Object illegal) {
			super(illegal.toString());
			this.illegal = illegal;
		}

		public Object getFirstIllegalArgument() {
			return illegal;
		}
	}

}
